<template>
  <div class="org-list">
    <div class="org-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>用户与权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>邀请用户</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="invite-user-container">
      <div class="invite-user-table">
        <div class="org-operation">
          <div>
            <span>所属机构</span>
            <el-cascader
              :options="organizationList"
              v-model="selectedOrgForDetail"
              :props="organizationListProps"
              change-on-select
              clearable
              size="mini"
            ></el-cascader>
          </div>
          <div class="org-position">
            <span>岗位</span>
            <el-select v-model="postId" placeholder="请选择岗位" size="mini">
              <el-option
                key=""
                label="全部"
                value="">
              </el-option>
              <el-option
                v-for="item in postList"
                :key="item.id"
                :label="item.postName"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="org-filter">
            <el-input v-model="filterStr" size="mini"></el-input>
          </div>
          <div class="org-label">共{{total}}条数据</div>
          <div>
            <el-button type="primary" size="mini" @click="onSearch()">搜索</el-button>
          </div>
        </div>
        <el-table
          :data="tableData"
          ref="multipleTable"
          border
          @selection-change="handleSelectionChange">
          style="width: 100%">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            prop="employeeId"
            label="员工编号">
          </el-table-column>
          <el-table-column
            prop="name"
            label="姓名">
          </el-table-column>
          <el-table-column
            prop="organizationName"
            label="所属机构">
          </el-table-column>
          <el-table-column
            prop="postName"
            label="岗位">
          </el-table-column>
          <el-table-column
            prop="phone"
            label="手机号"
            width="120">
          </el-table-column>
          <el-table-column
            prop="email"
            label="邮箱"
            width="120">
          </el-table-column>
        </el-table>
        <el-pagination
          style="float: right"
          layout="prev, pager, next"
          :total="total"
          @current-change="changePage">
        </el-pagination>
      </div>
      <div class="invite-user-list">
        <el-table
          :data="multipleSelection"
          border
          height="750">
          <el-table-column
            type="index"
            label="序号"
            width="60">
          </el-table-column>
          <el-table-column
            label="功能权限"
            width="260">
            <template slot-scope="scope">
              <div>
                <div>{{ scope.row.name + ' ' + scope.row.employeeId+ ' ' + scope.row.organizationName+ ' ' + scope.row.postName }}</div>
                <div>{{ scope.row.email }}</div>
                <div>{{ `登录账号 ${scope.row.phone}` }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="deleteInviteUser(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="org-label">已邀请{{ multipleSelection.length }}人</div>
      </div>
    </div>
    <div class="button-group">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" :disabled="multipleSelection.length === 0" @click="submit">下一步</el-button>
    </div>
  </div>
</template>

<script>
  import _ from 'lodash'
  export default {
    name: "invite-user",
    data() {
      return ({
        organizationList: [],
        organizationListProps: {
          children: 'children',
          label: 'name',
          value: 'id'
        },
        selectedOrgForDetail: [],
        filterStr: '',
        postId: '',
        postList: [],
        tableData: [],
        total: 0,
        selectedPerson: [],
      })
    },
    mounted() {
      this.getData()
      this.getOrganizationInfo()
      this.onSearch()
    },
    methods: {
      getData() {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        this.$http.post(this.GLOBAL.host, {
          "method":"organizationTree",
          "query":{}
        }).then(res => {
          loading.close()
          this.organizationList = res.data.data
        })
      },
      getOrganizationInfo() {
        this.$http.post(this.GLOBAL.host, {
          "method":"Emp_InitalData",
          "query":{}
        }).then(res => {
          this.postList = res.data.data.postBase
        })
      },
      changePage(pageNum) {
        this.updateData({
          inviteUserList: this.multipleSelection
        })
        this.onSearch(pageNum)
      },
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row, true);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      onSearch(pageNum, lastQuery) {
        const multipleSelection = this.multipleSelection
        this.lastQuery = lastQuery || {
          pageNo: pageNum || 1,
          pageSize: 10,
          organizationId: _.last(this.selectedOrgForDetail),
          postId: this.postId,
          keyword: this.filterStr,
        }
        return this.$http.post(this.GLOBAL.host, {
            method:"EmployeeList_NoUser",
            query: this.lastQuery,
          }
        ).then(res => {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        }).then(() => {
          // change page need update inviteUserList
          const hasSelectedData = _.filter(this.tableData, item =>
            _.some(multipleSelection, selectedItem => selectedItem.id === item.id)
          )
          if (hasSelectedData.length > 0) {
            this.selectedPerson = hasSelectedData
            this.updateData({
              inviteUserList: _.reject(this.$store.state.signForm.inviteUserList, item =>
                _.some(hasSelectedData, data => data.id === item.id)
              )
            })
          } else {
            this.selectedPerson = []
          }
          this.toggleSelection(hasSelectedData)
        })
      },
      handleSelectionChange(values) {
        this.selectedPerson = values
      },
      deleteInviteUser(row) {
        if (_.some(this.selectedPerson, person => person.id === row.id)) {
          this.$refs.multipleTable.toggleRowSelection(row);
          this.selectedPerson = _.reject(this.selectedPerson, item => item.phone === row.phone)
        } else {
          this.updateData({
            inviteUserList: _.reject(this.$store.state.signForm.inviteUserList, item =>
              row.id === item.id
            )
          })
        }
      },
      updateData(data) {
        this.$store.commit('updateSignForm', data)
      },
      cancel() {
        this.updateData({
          inviteUserList: []
        })
        this.selectedPerson = []
        this.$router.back()
      },
      submit() {
        this.updateData({
          inviteUserList: this.multipleSelection
        })
        this.selectedPerson = []
        this.$router.push('permission')
      }
    },
    computed: {
      multipleSelection: function() {
        return _.concat(this.$store.state.signForm.inviteUserList, this.selectedPerson)
      }
    },
  }
</script>

<style lang="less">
  .org-operation {
    display: flex;
    align-items: center;
    > div {
      margin-right: 15px;
      flex-shrink: 1;
    }
    margin-bottom: 10px;
    min-height: 32px;
  }
  .invite-user-container {
    display: flex;
  }
  .invite-user-table {
    flex-basis: 400px;
    flex-grow: 1;
    flex-shrink: 0;
  }
  .invite-user-list {
    flex: 400px 0 1;
    margin-left: 20px;
  }
  .org-position {
    > .el-select {
      width: 150px;
    }
  }
  .org-filter {
  }
  .org-list {
    padding: 10px 20px;
  }
  .button-group {
    text-align: center;
  }
</style>
